<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.16"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AngelScript: C++ exceptions and longjmp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(initResizable);
/* @license-end */</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.16 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('doc_cpp_exceptions.html','');});
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="PageDoc"><div class="header">
  <div class="headertitle">
<div class="title">C++ exceptions and longjmp </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h1><a class="anchor" id="doc_cpp_exceptions_1"></a>
Exceptions</h1>
<p>Application functions and class methods registered with the script engine are allowed to throw C++ exceptions. The virtual machine will automatically catch any C++ exception, abort the script execution, and return control to the application.</p>
<div class="fragment"><div class="line"><a class="code" href="classas_i_script_context.html">asIScriptContext</a> *ctx = engine-&gt;<a class="code" href="classas_i_script_engine.html#a2630e1cd03ffab0fee9b820bf0afe42a">CreateContext</a>();</div>
<div class="line">ctx-&gt;<a class="code" href="classas_i_script_context.html#a43976f42dfc6c1af23e132d36265173a">Prepare</a>(engine-&gt;<a class="code" href="classas_i_script_engine.html#a9f7cdc52b59034e6e55eb8a56b427aa4">GetModule</a>(<span class="stringliteral">&quot;test&quot;</span>)-&gt;<a class="code" href="classas_i_script_module.html#a81d727c9677b683942b6087df4ce95ad">GetFunctionByName</a>(<span class="stringliteral">&quot;func&quot;</span>));</div>
<div class="line"><span class="keywordtype">int</span> r = ctx-&gt;<a class="code" href="classas_i_script_context.html#a8e52894432737acac2e1a422e496bf84">Execute</a>();</div>
<div class="line"><span class="keywordflow">if</span>( r == <a class="code" href="angelscript_8h.html#a867f14b4137dd4602fda1e616b217a69aa3d548fa7d2278d848e50222b700c6c8">asEXECUTION_EXCEPTION</a> )</div>
<div class="line">{</div>
<div class="line">  <span class="keywordtype">string</span> err = ctx-&gt;<a class="code" href="classas_i_script_context.html#a46e2411bc84e99f57e7d9fe2374bb479">GetExceptionString</a>();</div>
<div class="line">  <span class="keywordflow">if</span>( err == <span class="stringliteral">&quot;Caught an exception from the application&quot;</span> )</div>
<div class="line">  {</div>
<div class="line">    <span class="comment">// An application function threw an exception while being invoked from the script</span></div>
<div class="line">    ...</div>
<div class="line">  }</div>
<div class="line">}</div>
</div><!-- fragment --><p>By default the VM has no way of distinguishing between different types of exceptions and will just give a standard exception string for all of them. If desired a <a class="el" href="classas_i_script_engine.html#addce6a202934a4918f7776b2f77c43fd">callback can be registered</a> with the engine to provide a translation of the exception type to a more informative exception string.</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> TranslateException(<a class="code" href="classas_i_script_context.html">asIScriptContext</a> *ctx, <span class="keywordtype">void</span>* <span class="comment">/*userParam*/</span>)</div>
<div class="line">{</div>
<div class="line">  <span class="keywordflow">try</span> </div>
<div class="line">  {</div>
<div class="line">    <span class="comment">// Retrow the original exception so we can catch it again</span></div>
<div class="line">    <span class="keywordflow">throw</span>;</div>
<div class="line">  }</div>
<div class="line">  <span class="keywordflow">catch</span>( std::exception &amp;e )</div>
<div class="line">  {</div>
<div class="line">    <span class="comment">// Tell the VM the type of exception that occurred</span></div>
<div class="line">    ctx-&gt;<a class="code" href="classas_i_script_context.html#a6f0f071215e0f7effc0b765043f01275">SetException</a>(e.what());</div>
<div class="line">  }</div>
<div class="line">  <span class="keywordflow">catch</span>(...)</div>
<div class="line">  {</div>
<div class="line">    <span class="comment">// The callback must not allow any exception to be thrown, but it is not necessary </span></div>
<div class="line">    <span class="comment">// to explicitly set an exception string if the default exception string is sufficient</span></div>
<div class="line">  }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Register the callback with the engine</span></div>
<div class="line">engine-&gt;<a class="code" href="classas_i_script_engine.html#addce6a202934a4918f7776b2f77c43fd">SetTranslateAppExceptionCallback</a>(<a class="code" href="angelscript_8h.html#a78f8f2c7f1c88b12e74a5ac47b4184ae">asFUNCTION</a>(TranslateException), 0, <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4">asCALL_CDECL</a>);</div>
</div><!-- fragment --><dl class="section see"><dt>See also</dt><dd><a class="el" href="doc_addon_helpers.html">GetExceptionInfo</a> helper function</dd></dl>
<dl class="section note"><dt>Note</dt><dd>The ability to catch exceptions can be turned off by compiling the library with the AS_NO_EXCEPTIONS defined. If this is done, the application should not register any functions that may throw exceptions, as the end result will be undefined should an exception occur.</dd></dl>
<h1><a class="anchor" id="doc_cpp_exceptions_2"></a>
longjmp</h1>
<p>Some applications uses longjmp to do error handling. When performing a longjmp to a previously saved state, there is no chance for the code to do a cleanup of all that happened after the saved state. Because of that the application must not register functions that can perform a longjmp out from the function, as that can leave the virtual machine in an undefined state. </p>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<div class="ttc" id="aclassas_i_script_context_html"><div class="ttname"><a href="classas_i_script_context.html">asIScriptContext</a></div><div class="ttdoc">The interface to the virtual machine.</div><div class="ttdef"><b>Definition:</b> angelscript.h:2654</div></div>
<div class="ttc" id="aclassas_i_script_engine_html_a9f7cdc52b59034e6e55eb8a56b427aa4"><div class="ttname"><a href="classas_i_script_engine.html#a9f7cdc52b59034e6e55eb8a56b427aa4">asIScriptEngine::GetModule</a></div><div class="ttdeci">virtual asIScriptModule * GetModule(const char *module, asEGMFlags flag=asGM_ONLY_IF_EXISTS)=0</div><div class="ttdoc">Return an interface pointer to the module.</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a8e52894432737acac2e1a422e496bf84"><div class="ttname"><a href="classas_i_script_context.html#a8e52894432737acac2e1a422e496bf84">asIScriptContext::Execute</a></div><div class="ttdeci">virtual int Execute()=0</div><div class="ttdoc">Executes the prepared function.</div></div>
<div class="ttc" id="aangelscript_8h_html_a867f14b4137dd4602fda1e616b217a69aa3d548fa7d2278d848e50222b700c6c8"><div class="ttname"><a href="angelscript_8h.html#a867f14b4137dd4602fda1e616b217a69aa3d548fa7d2278d848e50222b700c6c8">asEXECUTION_EXCEPTION</a></div><div class="ttdoc">The execution was terminated by an unhandled script exception.</div><div class="ttdef"><b>Definition:</b> angelscript.h:402</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a46e2411bc84e99f57e7d9fe2374bb479"><div class="ttname"><a href="classas_i_script_context.html#a46e2411bc84e99f57e7d9fe2374bb479">asIScriptContext::GetExceptionString</a></div><div class="ttdeci">virtual const char * GetExceptionString()=0</div><div class="ttdoc">Returns the exception string text.</div></div>
<div class="ttc" id="aclassas_i_script_module_html_a81d727c9677b683942b6087df4ce95ad"><div class="ttname"><a href="classas_i_script_module.html#a81d727c9677b683942b6087df4ce95ad">asIScriptModule::GetFunctionByName</a></div><div class="ttdeci">virtual asIScriptFunction * GetFunctionByName(const char *name) const =0</div><div class="ttdoc">Returns the function by its name.</div></div>
<div class="ttc" id="aangelscript_8h_html_a78f8f2c7f1c88b12e74a5ac47b4184ae"><div class="ttname"><a href="angelscript_8h.html#a78f8f2c7f1c88b12e74a5ac47b4184ae">asFUNCTION</a></div><div class="ttdeci">#define asFUNCTION(f)</div><div class="ttdoc">Returns an asSFuncPtr representing the function specified by the name.</div><div class="ttdef"><b>Definition:</b> angelscript.h:673</div></div>
<div class="ttc" id="aclassas_i_script_engine_html_a2630e1cd03ffab0fee9b820bf0afe42a"><div class="ttname"><a href="classas_i_script_engine.html#a2630e1cd03ffab0fee9b820bf0afe42a">asIScriptEngine::CreateContext</a></div><div class="ttdeci">virtual asIScriptContext * CreateContext()=0</div><div class="ttdoc">Creates a new script context.</div></div>
<div class="ttc" id="aclassas_i_script_engine_html_addce6a202934a4918f7776b2f77c43fd"><div class="ttname"><a href="classas_i_script_engine.html#addce6a202934a4918f7776b2f77c43fd">asIScriptEngine::SetTranslateAppExceptionCallback</a></div><div class="ttdeci">virtual int SetTranslateAppExceptionCallback(asSFuncPtr callback, void *param, int callConv)=0</div><div class="ttdoc">Register the exception translation callback.</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a6f0f071215e0f7effc0b765043f01275"><div class="ttname"><a href="classas_i_script_context.html#a6f0f071215e0f7effc0b765043f01275">asIScriptContext::SetException</a></div><div class="ttdeci">virtual int SetException(const char *info, bool allowCatch=true)=0</div><div class="ttdoc">Sets an exception, which aborts the execution.</div></div>
<div class="ttc" id="aangelscript_8h_html_a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4"><div class="ttname"><a href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4">asCALL_CDECL</a></div><div class="ttdoc">A cdecl function.</div><div class="ttdef"><b>Definition:</b> angelscript.h:226</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a43976f42dfc6c1af23e132d36265173a"><div class="ttname"><a href="classas_i_script_context.html#a43976f42dfc6c1af23e132d36265173a">asIScriptContext::Prepare</a></div><div class="ttdeci">virtual int Prepare(asIScriptFunction *func)=0</div><div class="ttdoc">Prepares the context for execution of the function.</div></div>
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Sun Sep 22 2019 11:47:54 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.16 </li>
  </ul>
</div>
</body>
</html>
